<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%
	String filePath = request.getParameter("filePath");
	if(filePath==null){
		filePath="";
	}
 %> 
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
<input type="hidden" id="filePath" value="<%=filePath %>"/>
<script type="text/javascript">
	//文件上传参数
	var upload_result_params = [];
	var upload_result_params1 = [];
	var upload_result_params2 = [];
	var upload_result_params3 = [];
    var upload_result_params4 = [];
	var serverPrefixPath = "";
	//页面加载
	$(function(){
		//添加上传组件
		$(".upfileBoxs").each(function(index, el){
		    var num = el.id.split("fileUpload")[1];
    		$(this).append(addUpfileBox(num));
    	});
		//获取服务器路径前缀
		$.ajax({
			type: 'get',
			url: '<%=path%>/system/sysulfilesrec/getServerPath.do',
			success:function(msg){
				serverPrefixPath = msg;
				console.log(serverPrefixPath);
			}
		});
	});
	
	//添加上传组件方法
	function addUpfileBox(num) {
		$Html=CreatUploadInputBox({
			id:"uploads" + num,  //id跟着序号走
			clearLine: true,
			deletable: false,
			finishClear: true //上传成功后清空文本框
		});
		return $Html;
	}
	
	//上传方法
	function ajaxFileUploadImg(fileId, fileIndex){
		var _filePath = $("#filePath").val();
		var wait_dialog=$.ligerDialog.SQalert({content: '上传中,请稍后...' ,SQ_type: "waitting" });
		//文件上传
		$.ajaxFileUpload({  
			url:'<%=path%>/system/sysulfilesrec/upload.do',
			secureuri:false,  
			fileElementId: fileId,  //上传框的id  
			dataType: 'json',  
			data: {filePath:_filePath},  
			success: function (msg){
				wait_dialog.close();
				if(msg.success == 1){
					$.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                    // 累计上传文件-json数组-数组合并
                    if (fileIndex == 1) {
                        upload_result_params1 = upload_result_params1.concat(msg.data);
                    } else if (fileIndex == 2) {
                        upload_result_params2 = upload_result_params2.concat(msg.data);
                    } else if (fileIndex == 3) {
                        upload_result_params3 = upload_result_params3.concat(msg.data);
                    } else if (fileIndex == 4) {
                        upload_result_params4 = upload_result_params4.concat(msg.data);
                    } else {
                        upload_result_params = upload_result_params.concat(msg.data);
                    }
					
					uploadCallbackForMore(msg.data);//上传回调方法
					
					showFiles(msg.data, fileIndex);//实时显示文件-上传成功后展示到页面上
					
					//遍历msg.data为filePath加上服务器路径前缀
            		if(serverPrefixPath.length >1 && serverPrefixPath.indexOf("\"")>=0){
            			serverPrefixPath = serverPrefixPath.substring(1,serverPrefixPath.length-1);
            		}
            		//var uploadJOSN = msg.data;
            		var uploadJOSN = JSON.parse(JSON.stringify(msg.data));
            		for (var i=0; i< uploadJOSN.length; i++){  
            			var oldPath = "";
            			var newPath = "";
            			oldPath = uploadJOSN[i].filePath;
            			newPath = serverPrefixPath + oldPath;
            			uploadJOSN[i].filePath = newPath;
            		}
            		//如果使用uploadJOSN = msg.data
            		//将直接改变upload_result_params的值-是指针导致的
                } else{
                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                }
            },
            complete: function(){
            	
            }
		});  
	}
	
	//获取上传结果参数
	function getUploadResultParams(fileIndex){
        // 根据序号返回不同的参数
        if (fileIndex == 1) {
            return upload_result_params1;
        } else if (fileIndex == 2) {
            return upload_result_params2;
        } else if (fileIndex == 3) {
            return upload_result_params3;
        } else if (fileIndex == 4) {
            return upload_result_params4;
        } else {
            return upload_result_params;
        }
	}
	
	//清除上传结果参数
    function clearUploadResultParams(fileIndex){
        // 根据序号清空不同的参数
        if (fileIndex == 1) {
            upload_result_params1 = [];
        } else if (fileIndex == 2) {
            upload_result_params2 = [];
        } else if (fileIndex == 3) {
            upload_result_params3 = [];
        } else if (fileIndex == 4) {
            upload_result_params4 = [];
        } else {
            upload_result_params = [];
        }
    }
	
	//上传回调方法
    function uploadCallbackForMore(_data){
		
    }

    // 删除文件
    function delFile(event, _fileId) {
        top.$.ligerDialog.confirm("确认删除吗?", function (flag) {
            if (!flag) {
                return;
            } else {
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: {'fileId': _fileId},
                    url: '<%=path%>/project/prjFileInfo/removeFile.do',
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({
                            content: LogUtils.LOG_CMM_DATA_LOADING,
                            SQ_type: "waitting"
                        });
                    },
                    success: function (msg) {
                        if (msg.success == 1) {
                            wait_dialog.close();
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            parent.$('li[tabid="mateTab"] a').text('项目资料(' + msg.data + ')');
                            event.stopPropagation();
                            var gridID = $(event.target).parents('.sq-grid').attr('ligeruiid');
                            liger.get(gridID).reload();
                        } else {
                            wait_dialog.close();
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            }
        });
    }
    
</script>
